<%@ page import="java.util.*" %>
<%@ page import="java.util.regex.*" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<%
if (request.getParameter("errmsg") != null) {
	String msg = request.getParameter("errmsg").replace("&lt;","<").replace("&gt;",">").replace("\r","");
	System.out.println(msg);
	if ((Boolean)request.getAttribute("adminRole")) {
		out.println(msg);
	}
	else {
		Pattern p = Pattern.compile("<h2>.+?<pre>(.+?)\n", Pattern.DOTALL);
		Matcher m = p.matcher(msg);
		if (m.find()) {
			out.println(m.group(0));
			out.println("</pre>");
		}
	}
}
else {
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
<style TYPE="text/css">
  table, th, td, pre {border: 1px solid #d7d7d7; border-collapse:collapse; font-size:12px;}
</style>
</head>
<body>

<h2>Internal Error</h2>
<p/>
<%
	out.println("<pre>");
	Throwable ex = (Throwable) request.getAttribute("javax.servlet.error.exception");
	out.println(ex.getMessage());
	out.println(ex.toString());
	out.println("</pre>");
%>
<%--
	try {
		out.println("<pre>");
		
		// The Servlet spec guarantees this attribute will be available
		Throwable exception = (Throwable) request.getAttribute("javax.servlet.error.exception");

		if (exception != null) {
			if (exception instanceof ServletException) {
				// It's a ServletException: we should extract the root cause
				ServletException se = (ServletException) exception;
				Throwable rootCause = se.getRootCause();
				if (rootCause == null)
					rootCause = se;
				out.println("** Root cause is: " + rootCause.getMessage());
				rootCause.printStackTrace(new java.io.PrintWriter(out));
			}
			else {
				// It's not a ServletException, so we'll just show it
				exception.printStackTrace(new java.io.PrintWriter(out));
			}
		}
		else {
			out.println("No available error information");
		}
		out.println("</pre>");

		out.println("<hr/>");
		
		// Display cookies
		out.println("<pre>");
		out.println("\nCookies:\n");
		Cookie[] cookies = request.getCookies();
		if (cookies != null) {
			for (int i = 0; i < cookies.length; i++) {
				out.println(cookies[i].getName() + "=[" + cookies[i].getValue() + "]");
			}
		}
		out.println("</pre>");
		
		out.println("<hr/>");
		
		// display request attributes
		out.println("<table>");
		Enumeration<String> enm = request.getAttributeNames();
		while (enm.hasMoreElements()) {
			String key = enm.nextElement();
			out.println("<tr><td>" + key + "</td><td>" + request.getAttribute(key) + "</td></tr>");
		}
		out.println("</table>");
		
		out.println("<hr/>");
		
		// display request headers
		out.println("<table>");
		Enumeration<String> henm = request.getHeaderNames();
		while (henm.hasMoreElements()) {
			String key = henm.nextElement();
			out.println("<tr><td>" + key + "</td><td>" + request.getHeader(key) + "</td></tr>");
		}
		out.println("</table>");

	}
	catch (Exception ex) {
		ex.printStackTrace(new java.io.PrintWriter(out));
	}
--%>

<p/>
<br/>

</body>
</html>
<%
}
%>
